<?php //pack11class

class NewPropertyResponder extends Module {

    public function setup() {
        $columnsData=MySQLGet::process("SHOW COLUMNS FROM ".MySQL::TABLE_PROPERTIES);
        $columns=array();
        $fields=array();
        $values=array();

        $systemSet=false;
        $commentDone=false;
        $comment="COMMENT";

        foreach($columnsData as $row) $columns[$row["Field"]]=$row["Field"];
        foreach($_POST as $name=>$value) {
            if(array_key_exists($name, $columns)) {
               if(strpos($name,"lang1")) {
                   $comment=$value;
                   $commentDone=true;
               }
               array_push($fields, "`".$name."`");
               array_push($values, "'".$value."'");
               if($name=="type" && ($value==2 || $value==4)) {
                    array_push($fields, "`isValue`");
                    array_push($values, "'1'");
               }
               if($name=="type" && $value==3) {
                    array_push($fields, "`hasValue`");
                    array_push($values, "'1'");
               }
               if($name=="system") {
                   $systemSet=true;
               }
            }
        }
        if(!$systemSet) {
            $nextID=MySQLGet::nextID(MySQL::TABLE_PROPERTIES);
            $system="system".$nextID;
            array_push($fields, "`system`");
            array_push($values, "'".$system."'");
        }
        if(!$commentDone) $comment=$system;

        $query="Insert into ".MySQL::TABLE_PROPERTIES." (".implode(",", $fields).") values (".implode(",", $values).");";
        $insert=MySQLSet::process($query);
        if(!$insert) return false;
        $id=MySQLSet::$id;
        $type=TypeManager::getType($_POST["type"]);
        if($type->id==3) {
            $newfield=MySQLSet::process("ALTER TABLE ".MySQL::TABLE_OBJECTS." ADD `prop{$id}` INT(100) NULL DEFAULT NULL COMMENT '{$comment}';");
            if(!$newfield) {
                echo MySQLSet::getQuery();
                MySQLSet::process("Delete from ".MySQL::TABLE_PROPERTIES." where `id`='".$id."';");
                return;
            }
            $newval=MySQLSet::process("ALTER TABLE ".MySQL::TABLE_OBJECTS." ADD `value{$id}` INT(100) NULL DEFAULT NULL COMMENT '{$comment}';");
            if(!$newval) {
                echo MySQLSet::getQuery();
                MySQLSet::process("Delete from ".MySQL::TABLE_PROPERTIES." where `id`='".$id."';");
                return;
            }
        } else if($type->id==4) {
            $newfield=MySQLSet::process("ALTER TABLE ".MySQL::TABLE_OBJECTS." ADD `prop{$id}` VARCHAR(255) NULL DEFAULT NULL COMMENT '{$comment}';");
            if(!$newfield) {
                echo MySQLSet::getQuery();
                MySQLSet::process("Delete from ".MySQL::TABLE_PROPERTIES." where `id`='".$id."';");
                return;
            }
        } else {
            $newfield=MySQLSet::process("ALTER TABLE ".MySQL::TABLE_OBJECTS." ADD `prop{$id}` INT(100) NULL DEFAULT NULL COMMENT '{$comment}';");
            if(!$newfield) {
                echo MySQLSet::getQuery();
                MySQLSet::process("Delete from ".MySQL::TABLE_PROPERTIES." where `id`='".$id."';");
                return;
            }
        }

        PropertiesManager::updateProperties();
        $property=PropertiesManager::getProperty($id);
        $typeName=$type->system;
        $itemClass="AdminProperty_".$typeName;
        $item=new $itemClass($property);
        $item->label=$property->label;
        $item->object=$_POST["object"];
        $item->num=$property->id;
        $item->property_name=$property->system;
        echo Template::html($item);
    }

}
?>
